package user

import (
	"context"
	"fmt"
	"git.mycaigou.com/gfyx/common/tenant_db"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	"git.myscrm.cn/golang/stark/v4"
)

type userBehaviorRepository struct {
	tenant_db.TenantDB
}

// NewUserBehaviorRepository 构造函数
func NewUserBehaviorRepository() UserBehaviorRepositoryIface {
	return &userBehaviorRepository{}
}

// SaveUserBehavior 保存记录
func (u *userBehaviorRepository) SaveUserBehavior(ctx context.Context, data *GfyxUserBehaviorPo) (int64, error) {
	data.ClientIP = fmt.Sprintf("%s", ctx.Value(constants.ClientIp))
	data.Device = fmt.Sprintf("%s", ctx.Value(constants.Device))
	result := u.GetTenantDB(ctx).Table(GfyxUserBehaviorPo{}.TableName()).Save(data)
	if result.Error != nil {
		stark.Logger.Errorf(ctx, "SaveUserBehavior save data : %+v, error: %+v", data, result.Error)
		return 0, result.Error
	}
	return data.ID, nil
}
